﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace WpfClient
{
    /// <summary>
    /// GradientWindow.xaml 的交互逻辑
    /// </summary>
    public partial class GradientWindow : Window
    {

        RadialGradientBrush brush;
        double angle;
        public GradientWindow()
        {
            InitializeComponent();

            // 1
            //LinearGradientBrush brush = new LinearGradientBrush();
            //brush.StartPoint = new Point(0, 0);
            //brush.EndPoint = new Point(1, 0);

            //RadialGradientBrush brush = new RadialGradientBrush();

            //this.Background = brush;

            //brush.GradientStops.Add(new GradientStop(Colors.Red, 0));
            //brush.GradientStops.Add(new GradientStop(Colors.Orange, .17));
            //brush.GradientStops.Add(new GradientStop(Colors.Yellow, .33));
            //brush.GradientStops.Add(new GradientStop(Colors.Green, .5));
            //brush.GradientStops.Add(new GradientStop(Colors.Blue, .67));
            //brush.GradientStops.Add(new GradientStop(Colors.Indigo, .84));
            //brush.GradientStops.Add(new GradientStop(Colors.Violet, 1));

            // 2
            //brush = new RadialGradientBrush(Color.FromRgb(237, 249, 243) , Color.FromRgb(191, 219, 255));
            //brush.RadiusX = brush.RadiusY = .1;
            //brush.SpreadMethod = GradientSpreadMethod.Repeat;

            //this.Background = brush;
            // 3
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            this.Width = 384;
            this.Height = 384;

            brush = new RadialGradientBrush(Color.FromRgb(237, 249, 243), Color.FromRgb(191, 219, 255));
            brush.Center = brush.GradientOrigin = new Point(0.5, 0.5);
            brush.RadiusX = brush.RadiusY = .1;
            brush.SpreadMethod = GradientSpreadMethod.Repeat;

            this.Background = brush;

            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(100);
            
            timer.Tick += (t, args) =>
            {
                Point pt = new Point(0.5 + 0.5 * Math.Cos(angle), 0.5 + 0.5 * Math.Sin(angle));

                brush.GradientOrigin = pt;

                angle += Math.PI / 6;
            };
                
            timer.Start();

        }

        void OnTimerTick(object sender, EventArgs args)
        {
            Point pt = new Point(0.5 + 0.5 * Math.Cos(angle), 0.5 + 0.5 * Math.Sin(angle));
            angle += Math.PI / 6;
        }

        //protected override void OnMouseDown(MouseButtonEventArgs e)
        //{
        //    base.OnMouseDown(e);

        //    double width = this.ActualWidth - 2 * SystemParameters.ResizeFrameHorizontalBorderHeight;
        //    double height = this.ActualHeight - 2 * SystemParameters.ResizeFrameVerticalBorderWidth - SystemParameters.CaptionHeight;

        //    Point ptMouse = e.GetPosition(this);
        //    ptMouse.X /= width;
        //    ptMouse.Y /= height;

        //    if (e.ChangedButton == MouseButton.Left)
        //    {
        //        brush.Center = ptMouse;
        //        brush.GradientOrigin = ptMouse;
        //    }
        //    else if(e.ChangedButton == MouseButton.Right)
        //    {
        //        brush.GradientOrigin = ptMouse;
        //    }
        //}
    }
}
